package com.ygqh.baby.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.crazycake.shiro.SerializeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.OrderDirection;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.dao.QuickReplyDao;
import com.ygqh.baby.model.QuickReply;
import com.ygqh.baby.redis.RedisDao;
import com.ygqh.baby.service.QuickReplyService;

@Service
public class QuickReplyServiceImpl implements QuickReplyService {
	
	@Autowired
	private QuickReplyDao quickReplyDao;
	@Autowired
	private RedisDao redisDao;
	
	private static final String QUICK_REPLY_KEY = "quick_reply_key";

	@Override
	public List<QuickReply> findQuickReplyList() {
		List<QuickReply> quickReplyList = this.findListFromRedis();
		if (CollectionUtils.isNotEmpty(quickReplyList)) {
			return quickReplyList;
		}
		return findListFromDB();
	}
	
	private List<QuickReply> findListFromDB() {
		Map<String, Object> conditions = new HashMap<>();
		conditions.put("status", DataStatus.Valid);
		Map<String, String> orders = new HashMap<>();
		orders.put("createTime", OrderDirection.desc.name());
		List<QuickReply> quickReplyList = quickReplyDao.getEntityList(conditions, orders);
		redisDao.set(QUICK_REPLY_KEY.getBytes(), SerializeUtils.serialize(quickReplyList), 0);
		return quickReplyList;
	}

	@SuppressWarnings("unchecked")
	private List<QuickReply> findListFromRedis() {
		byte[] bs = redisDao.get(QUICK_REPLY_KEY.getBytes());
		if (bs != null) {
			return (List<QuickReply>) SerializeUtils.deserialize(bs);
		}
		return null;
	}

	@Override
	public void save(QuickReply quickReply) {
		quickReplyDao.saveEntity(quickReply);
		this.findListFromDB();
	}

	@Override
	public void update(QuickReply quickReply) {
		quickReplyDao.updateEntity(quickReply);
		this.findListFromDB();
	}

	@Override
	public Message updateQuickIsFirst(String id, Boolean isFirst, DataStatus status) {
		if (StringUtils.isBlank(id) || (isFirst == null && status == null)) {
			return Message.error("请稍后重试");
		}
		Map<String, Object> updateMap = new HashMap<>();
		if (isFirst != null) {
			updateMap.put("isFirst", isFirst);
		}
		if (status != null) {
			updateMap.put("status", status);
		}
		quickReplyDao.updateEntity(updateMap, id);
		this.findListFromDB();
		return Message.success(1);
	}

	@Override
	public QuickReply findById(String id) {
		return quickReplyDao.findEntityById(id);
	}

	@Override
	public ResultSet<QuickReply> searchQuickReply(QueryInfo queryInfo, String title, DataStatus status) {
		List<QuickReply> userList = this.findQuickReplyList(queryInfo, title, status);
		Long count = this.quickReplyCount(title, status);
		return new ResultSet<>(count, userList);
	}

	private Long quickReplyCount(String title, DataStatus status) {
		Map<String, Object> conditions = new HashMap<>();
		if (StringUtils.isNotBlank(title)) {
			conditions.put("title", "^.*" + title + ".*$");
		}
		if (status != null) {
			conditions.put("status", status);
		} else {
			conditions.put("status", DataStatus.Valid);
		}
		return quickReplyDao.getEntityCount(conditions);
	}

	@Override
	public List<QuickReply> findQuickReplyList(QueryInfo queryInfo, String title, DataStatus status) {
		Map<String, Object> conditions = new HashMap<>();
		if (StringUtils.isNotBlank(title)) {
			conditions.put("title", "^.*" + title + ".*$");
		}
		if (status != null) {
			conditions.put("status", status);
		} else {
			conditions.put("status", DataStatus.Valid);
		}
		Map<String, String> orders = new HashMap<>();
		orders.put("createTime", OrderDirection.desc.name());
		return quickReplyDao.getPageEntityList(queryInfo, conditions, orders);
	}

	@Override
	public Message findWelcomeContent() {
		List<QuickReply> findListFromDB = this.findListFromDB();
		if (CollectionUtils.isEmpty(findListFromDB)) {
			return Message.error("暂无欢迎语");
		}
		List<QuickReply> list = new ArrayList<>();
		findListFromDB.stream().forEach(reply -> {
			if (reply.getIsFirst()) {
				list.add(reply);
			}
		});
		if (CollectionUtils.isEmpty(list)) {
			return Message.error("暂无欢迎语");
		}
		return Message.success(list);
	}
	
}
